package com.banciyuan.bcywebview.biz.main.newdiscover.ranklist;

import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.Card;


public class InterestRankAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<Card> mCardList;
    public static final int DAILY_TYPE = 0;
    public static final int LONG_DAILY_TYPE = 1;
    public static final int VIDEO_TYPE = 2;

    public InterestRankAdapter(Context context, List<Card> cardList) {
        mContext = context;
        mCardList = cardList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case DAILY_TYPE:
            default:
                return new DailyViewHolder(mLayoutInflater.inflate(R.layout.interesttag_item_daily, parent, false));
            case LONG_DAILY_TYPE:
                return new LongDailyViewHolder(mLayoutInflater.inflate(R.layout.interesttag_item_longdaily, parent, false));
            case VIDEO_TYPE:
                return new VideoViewHolder(mLayoutInflater.inflate(R.layout.interesttag_item_video, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case DAILY_TYPE:
                setDailyUi((DailyViewHolder) holder, mCardList.get(position));
                break;
            case LONG_DAILY_TYPE:
                setLongDailyUi((LongDailyViewHolder) holder, mCardList.get(position));
                break;
            case VIDEO_TYPE:
                setVideoUI((VideoViewHolder) holder, mCardList.get(position));
                break;
            default:
                break;
        }
    }

    private void setVideoUI(VideoViewHolder holder, Card card) {
        ImageLoader.getInstance().displayImage(card.getCover(), holder.ivVideoCover, ImageOptions.options);
        if (!TextUtils.isEmpty(card.getUname())) {
            holder.tvUname.setText(card.getUname());
        } else {
            holder.tvUname.setText("");
        }
        OnClick onClick = new OnClick(card);
        holder.tvUname.setOnClickListener(onClick);
        holder.ivVideoCover.setOnClickListener(onClick);
    }

    private void setLongDailyUi(LongDailyViewHolder holder, Card card) {
        if (!TextUtils.isEmpty(card.getTitle()))
            holder.tvLongDailyTitle.setText(card.getTitle());
        if (!TextUtils.isEmpty(card.getCover())) {
            ImageLoader.getInstance().displayImage(card.getCover(), holder.ivLongDailyCover, ImageOptions.options);
        } else {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                holder.ivLongDailyCover.setImageDrawable(mContext.getResources().getDrawable(R.drawable.larticle_cover, mContext.getTheme()));
            } else {
                holder.ivLongDailyCover.setImageDrawable(mContext.getResources().getDrawable(R.drawable.larticle_cover));
            }
        }
        if (!TextUtils.isEmpty(card.getUname())) {
            holder.tvUname.setText(card.getUname());
        } else {
            holder.tvUname.setText("");
        }
        OnClick onClick = new OnClick(card);
        holder.tvUname.setOnClickListener(onClick);
        holder.ivLongDailyCover.setOnClickListener(onClick);
    }

    private void setDailyUi(DailyViewHolder holder, Card card) {
        if (!TextUtils.isEmpty(card.getImg_src())) {
            holder.ivDailyCover.setVisibility(View.VISIBLE);
            holder.tvDailyText.setVisibility(View.GONE);
            holder.dailyNopicIcon.setVisibility(View.GONE);
            holder.viewDaily.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            ImageLoader.getInstance().displayImage(card.getImg_src(), holder.ivDailyCover, ImageOptions.options);
        } else {
            holder.ivDailyCover.setVisibility(View.INVISIBLE);
            holder.tvDailyText.setVisibility(View.VISIBLE);
            holder.dailyNopicIcon.setVisibility(View.VISIBLE);
            holder.viewDaily.setBackgroundColor(mContext.getResources().getColor(R.color.small_card_bg));
            if (!TextUtils.isEmpty(card.getPlain())) {
                holder.tvDailyText.setText(Html.fromHtml(card.getPlain()));
            }
        }
        if (!TextUtils.isEmpty(card.getUname())) {
            holder.tvUname.setText(card.getUname());
        } else {
            holder.tvUname.setText("");
        }
        OnClick onClick = new OnClick(card);
        holder.tvUname.setOnClickListener(onClick);
        holder.viewDaily.setOnClickListener(onClick);
    }

    @Override
    public int getItemCount() {
        return mCardList.size();
    }

    @Override
    public int getItemViewType(int position) {
        String type = mCardList.get(position).getType();
        if ("daily".equals(type))
            return DAILY_TYPE;
        if ("larticle".equals(type))
            return LONG_DAILY_TYPE;
        if ("svideo".equals(type))
            return VIDEO_TYPE;
        return DAILY_TYPE;
    }

    private class DailyViewHolder extends EventBottomViewHolder {
        ImageView ivDailyCover;
        TextView tvDailyText;
        View dailyNopicIcon;
        View viewDaily;

        DailyViewHolder(View itemView) {
            super(itemView);
            ivDailyCover = (ImageView) itemView.findViewById(R.id.siv_daily_item);
            tvDailyText = (TextView) itemView.findViewById(R.id.daily_text);
            dailyNopicIcon = itemView.findViewById(R.id.iv_daily_nopic_icon);
            viewDaily = itemView.findViewById(R.id.view_daily_item);
        }
    }

    private class LongDailyViewHolder extends EventBottomViewHolder {
        ImageView ivLongDailyCover;
        TextView tvLongDailyTitle;

        LongDailyViewHolder(View itemView) {
            super(itemView);
            ivLongDailyCover = (ImageView) itemView.findViewById(R.id.siv_item_longdaily);
            tvLongDailyTitle = (TextView) itemView.findViewById(R.id.tv_longdaily_title);
        }
    }

    private class VideoViewHolder extends EventBottomViewHolder {
        ImageView ivVideoCover;

        VideoViewHolder(View itemView) {
            super(itemView);
            ivVideoCover = (ImageView) itemView.findViewById(R.id.siv_item_video);
        }
    }

    private class EventBottomViewHolder extends RecyclerView.ViewHolder {
        TextView tvUname;

        EventBottomViewHolder(View itemView) {
            super(itemView);
            tvUname = (TextView) itemView.findViewById(R.id.tv_uname);
        }
    }

    class OnClick implements View.OnClickListener {
        Card mItem;

        public OnClick(Card item) {
            mItem = item;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.view_daily_item:
                case R.id.siv_item_video:
                case R.id.siv_item_longdaily:
                    DetailDataHelper.gotoDailyDetail(mContext, mItem.getUd_id(), false);
                    break;
                case R.id.tv_uname:
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, mItem.getUid());
                    break;
            }
        }
    }
}
